package org.exussum0.eyecks1;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

public class FsTraverser {
	public static interface Visitor {
		public void visit(File file) throws Exception;
	}

	public void traverse(File aStartingDir, Visitor visitor) throws Exception {
		visitFiles(new HashSet<File>(), aStartingDir, visitor);
	}

	public void visitFiles(Set<File> visited, File directory, Visitor visitor)
			throws Exception {
		File[] filesAndDirs = directory.listFiles();
		if (filesAndDirs != null)
			for (File file : filesAndDirs) {
				visitor.visit(file);
				if (!visited.contains(file) && file.isDirectory()) {
					visited.add(file);
					visitFiles(visited, file, visitor);
				}
			}
	}
}